package cn.kgc.web.service;

import cn.kgc.common.entity.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * Created by teacher ZHANG on 2023/2/13
 */
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${rest.search.url}")
    private String searchUrl;

    @Override
    public List<Article> searchArticles(String keyword, String fromDate, String endDate, Integer pageNum) {
        String fmt = searchUrl + "?keyword=%s&fromDate=%s&endDate=%s&pageNum=%d";

        String url = String.format(fmt, keyword, fromDate, endDate, pageNum);
        Article[] articles = restTemplate.getForObject(url, Article[].class);

        return Arrays.asList(articles);
    }
}
